package com.fengba.nanzhi.notice;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.login.Login;
import com.fengba.nanzhi.myfengba.MyCF;
import com.fengba.nanzhi.myfengba.MyDH;
import com.fengba.nanzhi.utils.FX_SignInUtil;
import com.fengba.nanzhi.utils.OKManager;
import com.fengba.nanzhi.utils.Preferences;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by SpakeBrony on 2018/4/13.
 */

public class Notice extends Activity {


    private RecyclerView mCardyhk;
    private JSONArray jsonArray1;

    public Notice() {
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fx_notice);

        /**
         * 返回
         */
        ImageButton Nbtn01 = (ImageButton)findViewById(R.id.Nbtn01);
        Nbtn01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        final RefreshLayout refreshLayout = (RefreshLayout) findViewById(R.id.NrefreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {

            public void onRefresh(RefreshLayout refreshlayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        if (jsonArray1 == null) {
                            Toast.makeText(Notice.this, "已经没有更多数据了", Toast.LENGTH_SHORT).show();
                            refreshLayout.finishRefresh();
                        } else {
                            init();
                            Toast.makeText(Notice.this, "查询成功", Toast.LENGTH_SHORT).show();
                            refreshLayout.finishRefresh();
                        }
                        /**
                         * 刷新完成后调用此方法，要不然刷新效果不消失
                         */

                    }
                }, 0);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });

        init();



    }//都在这里打

    public void init() {
//        final CountDownLatch latch = new CountDownLatch(1);
//        OkHttpClient client = new OkHttpClient();
//        FormBody formBody = new FormBody
//                .Builder()
//                .add("account", "17751567370")
//                .add("pageNum", "1")
//                .add("pageSize", "100")
//                .build();
//        final Request request = new Request.Builder()
//                .url(UrlUtil.UrlFX + "api/user/messagelist")
//                .post(formBody)
//                .build();
//        Call call = client.newCall(request);
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                latch.countDown();
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//
//
//                //final String responseStr = response.body().string();
//                //ToastUtil.showToast(PostParameterActivity.this, "Code：" + String.valueOf(response.code()));
//                try {
//                    String json = response.body().string();
//                    JSONObject jsonObject = new JSONObject(json);
//                    if (jsonObject.get("code").equals(0)) {
//                        jsonArray1 = jsonObject.getJSONArray("data");
//                        //System.out.println("JA"+jsonArray1.toString());
//                    } else {
//                        Toast.makeText(Notice.this, "网络错误", Toast.LENGTH_SHORT).show();
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//                latch.countDown();
//            }
//        });
//        try {
//            latch.await();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        String account = null;
        try {
            Object user_info = Preferences.getParam(Notice.this, "user_info", "");
            JSONObject myJson = null;
            myJson = new JSONObject(user_info.toString());
            account = myJson.get("account").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (FX_SignInUtil.switchAccount(Notice.this)) {
            startActivity(new Intent(Notice.this, Login.class));
        } else {
            OKManager.getInstance().type2001(account, "1", "100", new OKManager.func4() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    try {
                        if (jsonObject.get("code").equals(0)) {
                            jsonArray1 = jsonObject.getJSONArray("data");
                            //System.out.println("JA" + jsonArray1.toString());
                            mCardyhk = findViewById(R.id.Nshdzlb);
                            mCardyhk.setLayoutManager(new LinearLayoutManager(Notice.this));
                            mCardyhk.setAdapter(new NoticeAdapter(Notice.this, Notice.this));
                        } else {
                            //jsonArray1 = jsonObject.getJSONArray("data");
                            Toast.makeText(Notice.this, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    //refreshLayout.finishRefresh();
                }

                @Override
                public void onFailure(String string) {
                    Toast.makeText(Notice.this, "网络错误", Toast.LENGTH_SHORT).show();
                    //refreshLayout.finishRefresh();
                }
            });
        }
    }
    public JSONArray getJsonArray() {
        return jsonArray1;
    }
}